<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\SchoolArticleStoreRequest;
use App\Http\Requests\Admin\SchoolStoreRequest;
use App\Models\Qh_SchoolArticle;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SchoolArticleController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Qh_SchoolArticle::filter(\request()->all())
            ->with(['school' => function($q) {
                $q->select(['id', 'name']);
            }])
            ->orderByDesc('id')
            ->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param SchoolArticleStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(SchoolArticleStoreRequest $request)
    {
        $params = $request->validated();
        $school = Qh_SchoolArticle::create($params);
        return $this->restful($school);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param SchoolArticleStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(SchoolArticleStoreRequest $request, int $id)
    {
        $params = $request->validated();
        $result = Qh_SchoolArticle::where('id', $id)->update($params);
        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $result = Qh_SchoolArticle::where('id', $id)->delete();

        return $this->restful($result);
    }
}
